package com.example.android.ui.me.user;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.Transformations;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.android.R;
import com.example.android.adapter.CarAdapter;
import com.example.android.adapter.CarReleaseAdapter;
import com.example.android.adapter.CarReleaseReviewAdapter;
import com.example.android.bean.BaseModelTable;
import com.example.android.bean.CarRelease;
import com.example.android.utils.BaseModel;
import com.example.android.utils.ImageUrlUtil;
import com.example.android.utils.NetUtil;
import com.example.android.utils.api.CarReleaseApi;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class reviewFragment extends Fragment {
    private CarReleaseReviewAdapter carReleseAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_review, container, false);
        RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(root.getContext(), DividerItemDecoration.VERTICAL));
        carReleseAdapter = new CarReleaseReviewAdapter(null);
        recyclerView.setAdapter(carReleseAdapter);
        //添加单击按钮
        carReleseAdapter.addChildClickViewIds(R.id.yuyue,R.id.profile_image);

        //设置单击事件
        carReleseAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            CarRelease item = (CarRelease) adapter.getItem(position);
            Log.i("TAG", "onCreateView: " + item.toString());
            switch (view.getId()) {
                case R.id.yuyue:
                    //管理员点击预约操作
                    AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
                    builder.setTitle("发布信息信息审核");
                    builder.setMessage("是否审核通过？");
                    builder.setNegativeButton("取消", null);
                    builder.setPositiveButton("通过", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(CarReleaseApi.class).reviewRelease(item.getId()).enqueue(new Callback<BaseModel<String>>() {
                                @Override
                                public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                                    Log.i("TAG", "onResponse: " + response.toString());
                                    getList();
                                }
                                @Override
                                public void onFailure(Call<BaseModel<String>> call, Throwable t) {

                                }
                            });
                        }
                    });
                    builder.show();
                    break;
                case R.id.profile_image:
                    String imagesUrl = ImageUrlUtil.getImagesUrl(item.getCarInfo());
                    if (imagesUrl != null) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", imagesUrl);
                        Navigation.findNavController(view).navigate(R.id.action_navigation_home_to_webFragment, bundle);
                    }
                    break;
            }
        });

        getList();
        return root;
    }

    private void getList() {
        CarRelease carRelease = new CarRelease();
        carRelease.setStatus(1);
        NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(CarReleaseApi.class).getPublicRelease(carRelease).observe(getViewLifecycleOwner(), carReleaseBaseModelTable -> {
            if (carReleaseBaseModelTable != null && carReleaseBaseModelTable.getRows() != null) {
                carReleseAdapter.setList(carReleaseBaseModelTable.getRows());
            }
        });
    }
}